package com.hxf.web.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import com.hxf.common.AppProps;
import com.hxf.common.DateUtil;
import com.hxf.common.ImageUtils;

public class ImagesActionBase extends WebActionBase {
	/**
	 * 生成图片保存路径
	 * @param date
	 * @param saveName
	 * @return
	 */
	protected File saveRealFile(Date date, String saveName) {
		AppProps.load();
		// 保存在服务器中的物理文件夹地址
		String path =  AppProps.getString("image.upload.path");
		String folder = AppProps.getString("image.upload.folder");
		File saveFolder = new File(path+folder, DateUtil.createDateFolder(date));
		if (!saveFolder.exists()) {
			saveFolder.mkdirs();
		}

		// 保存在服务器中的文件的真实地址
		File saveFile = new File(saveFolder, saveName);
		return saveFile;
	}
	
	
	
	/**
	 * 获取相对路径
	 * @param date
	 * @param folder
	 * @param saveName
	 * @return
	 */
	protected String getAbsPath(Date date, String saveName) {
		return AppProps.getString("image.upload.folder")+DateUtil.createDateFolder(date) + saveName;
	} 
	
	
	/**
	 * 上传图片
	 * @param file
	 * @param fileName
	 * @return
	 * @throws IOException 
	 */
	protected String uploadFile(File file, String fileName, int width, int height) throws IOException {
		int index = fileName.lastIndexOf(".");
		String subString = fileName.substring(index, fileName
				.length());
		String saveName = System.currentTimeMillis() + subString;
		Date date = new Date(); 
		System.out.println(width + "150 100----------==========================---------" + height);
		
		file = beforeSave(file, 200, 300);
		// 保存文件
		File saveFile = saveRealFile(date,  saveName);
		//File appimgFile = saveRealFileAppImg(date, saveName);
		FileUtils.copyFile(file, saveFile);
		
		//File appFile = beforeSave(file, 260, 100);
		//FileUtils.copyFile(appFile, appimgFile);
		
		return getAbsPath(date, saveName);
	}
	
	/**
	 * 上传图片
	 * @param file
	 * @param fileName
	 * @return
	 * @throws IOException 
	 */
	protected String uploadFile(File file, String fileName) throws IOException {
		int index = fileName.lastIndexOf(".");
		String subString = fileName.substring(index, fileName
				.length());
		String saveName = System.currentTimeMillis() + subString;
		Date date = new Date();
		//file = beforeSave(file, 540, 300);
		// 保存文件
		File saveFile = saveRealFile(date,  saveName);
		
		FileUtils.copyFile(file, saveFile);
		
		return getAbsPath(date, saveName);
	}
	
	/**
	 * 生成前端APP图片保存路径
	 * @param date
	 * @param saveName
	 * @return
	 */
	protected File saveRealFileAppImg(Date date, String saveName) {
		AppProps.load();
		// 保存在服务器中的物理文件夹地址
		String path =  AppProps.getString("image.upload.path");
		String folder = AppProps.getString("image.upload.appimg");
		File saveFolder = new File(path+folder, DateUtil.createDateFolder(date));
		if (!saveFolder.exists()) {
			saveFolder.mkdirs();
		}

		// 保存在服务器中的文件的真实地址
		File saveFile = new File(saveFolder, saveName);
		return saveFile;
	}
	
	/**
	 * 获取app图片相对路径
	 * @param date
	 * @param folder
	 * @param saveName
	 * @return
	 */
	protected String getAppAbsPath(Date date, String saveName) {
		return AppProps.getString("image.upload.folder")+DateUtil.createDateFolder(date) + saveName;
	}
	
	/**
	 * 上传App图片
	 * @param file
	 * @param fileName
	 * @return
	 * @throws IOException 
	 */
	protected String uploadAppFile(File file, String fileName, int width, int height) throws IOException {
		int index = fileName.lastIndexOf(".");
		String subString = fileName.substring(index, fileName
				.length());
		String saveName = System.currentTimeMillis() + subString;
		Date date = new Date();  
		
		File appimgFile = saveRealFileAppImg(date, saveName);
		File appFile = beforeSave(file, width, height);
		//File appFile = beforeSave(file,150,100);
		FileUtils.copyFile(appFile, appimgFile);
		
		return getAppAbsPath(date, saveName);
	}
	
	/**
	 * 上传App图片
	 * @param file
	 * @param fileName
	 * @return
	 * @throws IOException 
	 */
	protected String uploadAppFile(File file, String fileName) throws IOException {
		int index = fileName.lastIndexOf(".");
		String subString = fileName.substring(index, fileName
				.length());
		String saveName = System.currentTimeMillis() + subString;
		Date date = new Date(); 
		// 保存文件
		File saveFile = saveRealFileAppImg(date,  saveName);
		
		FileUtils.copyFile(file, saveFile);
		
		return getAppAbsPath(date, saveName);
	}
	
	/**
	 * 图片剪切
	 * @param file
	 * @return
	 */
	protected File beforeSave(File file, int width, int height) {
		String path = file.getAbsolutePath();

		if (width <= 0) {
			width = 93;
		}

		if (height <= 0) {
			height = 90;
		}
		//width = 100;
		//height = 150;
		ImageUtils.reduceImg(path, width, true);
		ImageUtils.reduceImg(path, height, false);

		return new File(path);
	}
	
	
}
